/**
 * Created by bolin on 2016/4/26.
 */
package team.zero.lemon.common.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import team.zero.lemon.common.exception.AuthException;
import team.zero.lemon.common.exception.LoginException;
import team.zero.lemon.common.exception.CustomException;
import team.zero.lemon.common.exception.PermissionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 控制器支持类
 *
 * @author bolin
 * @version 2013-3-23
 */

public abstract class BaseController {
    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler
    public String loginException(HttpServletRequest request, HttpServletResponse response, RuntimeException e) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        if (e instanceof LoginException) {
            response.setStatus(600);
        } else if (e instanceof CustomException) {
            response.setStatus(602);
        } else if (e instanceof AuthException) {
            response.setStatus(604);
        }else if (e instanceof PermissionException) {
            response.setStatus(605);
        }else if (e instanceof org.springframework.web.multipart.MaxUploadSizeExceededException) {
            //sb.append("文件大小不应大于"+((MaxUploadSizeExceededException)ex).getMaxUploadSize()/1000+"kb");
            response.setStatus(609);
        }
        PrintWriter writer = response.getWriter();
        logger.error(e.getMessage());
        writer.write(e.getMessage());
        return null;
    }

}
